import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/common/widget/dialog/m_dialog.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/main/app.dart';
import 'package:bike_ride/model/cyling/model/unpay_info_m.dart';
import 'package:bike_ride/model/scan/model/scan_m.dart';
import 'package:bike_ride/router/navigator_util.dart';
import 'package:bike_ride/router/routers.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';

// This is our generated file (we'll see this soon!)
part 'unpay.g.dart';

// We expose this to be used throughout our project
class UnPay = _UnPay with _$UnPay;

abstract class _UnPay with Store {

  @observable
  String mainViewText = '';

  @observable
  String dialogViewText = '';
  @observable
  String confirmName = '';

  @observable
  List<UnPayInfoM> unPayOrderList = [];

  @observable
  List<ViolationUnPay> violationList = [];

  bool isLoading = false;
  Future<dynamic> onEnter() async{
    if(unPayOrderList.isNotEmpty){
      if(unPayOrderList.length > 1){
        return NavigatorUtil.push(navigatorKey.currentContext!, Routers.cyclingRecordView).then((_)=>true);
      }else {
        ///只有一个订单
        final ridingCount = unPayOrderList.where((element) => element.lockTime == null || element.lockTime == 0).length;

        if (ridingCount > 0) {
          return NavigatorUtil.push(navigatorKey.currentContext!, Routers.cyclingView,
              arguments: {
                'order_id': unPayOrderList.first.orderId,
              }).then((_)=>true);
        } else {
          return NavigatorUtil.push(navigatorKey.currentContext!, Routers.payView,
              arguments: {
                'order_id': unPayOrderList.first.orderId,
              }).then((_)=>true);
        }
      }
    }else if(violationList.isNotEmpty){
      /// 单个订单 跳转详情 处理违章
      if(violationList.length == 1){
        return NavigatorUtil.push(navigatorKey.currentContext!, Routers.payView,
            arguments: {
              'order_id': violationList.first.linkId,
            }).then((_)=>true);
      }
      ///多个订单 跳转列表 (第一版暂不需要)
    }
  }

  @action
  Future getUnPayOrder({bool showDialog = true}) async {
    if(isLoading)return;
    isLoading = true;
    final result = await CommonRepository.getUnPayOrderIdl();
    isLoading = false;
    unPayOrderList = result;
    if(result.isNotEmpty){
      final ridingCount = result.where((element) => element.lockTime == null || element.lockTime == 0).length;
      final unPayCount = result.length - ridingCount;

      if(ridingCount > 0){
        mainViewText = '${S.current.youHave}${ridingCount==1?'':'$ridingCount${S.current.pen}'}${S.current.runningPay}';
        dialogViewText = '$mainViewText,${S.current.awaitFind}';
        confirmName = S.current.goCheck;
      }else{
        mainViewText = '${S.current.youHave}${unPayCount==1?'':'$unPayCount${S.current.pen}'}${S.current.needToPay}';
        dialogViewText = '$mainViewText,${S.current.awaitPay}';
        confirmName = S.current.goRefund;
      }
      if(showDialog){
        showTipDialog();
      }
    }else {
      getUnPayViolation();
    }
  }

  @action
  Future getUnPayViolation() async {
    if(!AccountManager.isLogin) return;
    final result = await CommonRepository().getUnPayViolationIdl();
    violationList = result;
    if(result.isNotEmpty){
      mainViewText = S.current.violationNeedToPay;
      dialogViewText = '$mainViewText,${S.current.awaitPay}';
      confirmName = S.current.goCheck;
    }
  }

  void showTipDialog(){
    /// 未支付弹窗
    showCupertinoDialog(context: navigatorKey.currentContext!, builder: (context){
      return CupertinoAlertDialog(
          title: Text(S.current.dialogTitle),
          content: Text(dialogViewText,style: TextStyle(fontSize: 30.rpx, color: Colors.black87),),
          actions: [
            CupertinoDialogAction(
              child: Text(S.current.cancel,style: const TextStyle(color: Colors.black),),
              onPressed: (){
                Navigator.of(context).pop(false);
              },
            ),
            CupertinoDialogAction(
                child: Text(confirmName),
                onPressed: (){
                  Navigator.of(context).pop();
                  onEnter().then((value){
                    if(value != null){
                      getUnPayOrder();
                    }
                  });
                }
            )
          ]
      );
    });
  }
}